package javabase.schoolwork.day6_1to3;

import java.util.List;
import java.util.Scanner;

/**
 * @author fance
 * @date 2018/6/2 18:42
 */
public class ATM {
    private Bank ownerBank;
    private Account curAccount;
    public ATM(Bank ownerBank) {
        this.ownerBank = ownerBank;
        this.ownerBank.init();
    }



    public  void menu() {
        System.out.println("欢迎使用招商银行ATM系统");
        System.out.println("-----------------------------");
        System.out.println("1:用户登录");
        System.out.println("2:查询余额");
        System.out.println("3:存钱");
        System.out.println("4:取钱");
        System.out.println("0:退出系统");
        System.out.println("-----------------------------");
        System.out.println("请选择");
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int choose = scanner.nextInt();
            switch (choose) {
                case 0:
                    System.out.println("欢迎下次使用");return;
                case 1: if (login(scanner.next(),scanner.next())){
                    System.out.println("登录成功");
                    menu();
                } else {
                    System.out.println("登录失败");
                    menu();
                } break;
                case 2:
                    if (getbalance()) {
                        System.out.println("查询余额成功");
                        menu();
                    } else {
                        System.out.println("查询余额失败");
                        menu();
                    } break;
                case 3:if (deposit(scanner.nextDouble())){
                    System.out.println("存钱成功");
                    menu();
                } else {
                    System.out.println("存钱失败");
                    menu();
                } break;
                case 4:if (withdaw(scanner.nextDouble())){
                    System.out.println("取钱成功");
                    menu();
                } else {
                    System.out.println("取钱失败");
                    menu();
                }break;
            }
        }
    }
    public  boolean login(String name,String password) {
        List<Account> accountList = ownerBank.getAccounts();
        for (Account account : accountList) {
            if (account.getName().equals(name) && account.getPassword().equals(password)) {
                this.curAccount = account;
                return true;
            }
        }
        return false;
    }

    public boolean deposit(double money) {
        if (check()) {
            return curAccount.deposit(money);
        }
        return false;
    }
    public boolean withdaw(double money) {
        if (check()) {
            return curAccount.withdaw(money);
        }
        return false;
    }
    public boolean getbalance() {
        if (check()) {
            System.out.println(curAccount.getBalance());
            return true;
        }
        return false;
    }

    private boolean check() {
        if (curAccount == null) {
            System.out.println("请先登录。。。");
            return false;
        }
        return true;
    }
}
